﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGS.Cards;

namespace SGS.Standard.GameLogic
{
    /// <summary>
    /// 基本牌逻辑 & 装备牌逻辑
    /// </summary>
    internal class Basic : GameLogicBase
    {
        #region 杀
        internal static void GL_Bang(ActorBase actor, Player from, List<Player> targets, List<Card> cards)
        {
            cards = cards ?? new List<Card>();
            targets = targets ?? new List<Player>();

            if (!GL_CheckBangTarget(targets))
            {
                RaiseNoticeMessageEvent( "{0} Bang Target({1}) invalid", from.PlayerName, string.Join(",", targets.Select(p => p.PlayerName).ToArray()));
                return;
            }

            // 检查距离
            var distanceColl = PlayerDistanceCollection.Calc(GameContext.Current.PlayersCircle, from);
            // int attackDistance = from.GameInfo.AttackRange;
            foreach (var target in targets)
            {
                int d = distanceColl.GetDistance(from, target);
                if (from.GameInfo.AttackRange < d)
                {
                    GameContext.Current.RaiseNoticeMessageEvent(GameContext.Current, "{0} AttachRange is {1}, but Target {2} distance is {3}",
                        from.PlayerName, from.GameInfo.AttackRange, target.PlayerName, d);
                    return;
                }
            }

            if (!from.GameInfo.TryRemoveCard(cards))
            {
                RaiseNoticeMessageEvent("Bang Card is not exists ");
            }

            RaiseNoticeMessageEvent("{0} 对目标{1} 使用了 {3} 颜色为 {2}", from.PlayerName, string.Join(",", targets.Select(p => p.PlayerName).ToArray()),
               CardLogicHelper.CheckCardColor(cards.ToArray()), CardLogicHelper.CheckBangType(cards.ToArray()));

            foreach (var target in targets)
            {
                // GL_OnAskingTargetUseMiss();

                var result = Ask.GL_AskMiss(target, actor);
                switch (result.AnswerResult)
                {
                    case AskUseCardResult.AskResult.Cancel:
                        Core.GL_Damage(from, target, DamageType.Default, cards);
                        break;
                    case AskUseCardResult.AskResult.Used:
                        RaiseNoticeMessageEvent("{0} 使用了闪", target.PlayerName);
                        Core.GL_UsedDiscard(target, result.UsedCard);
                        break;
                    case AskUseCardResult.AskResult.Skip:
                        break;
                }
            }

            Core.GL_UsedDiscard(from, cards);
        }

        internal static bool GL_CheckBangTarget(List<Player> target)
        {
            if (target.Count == 0)
            {
                RaiseNoticeMessageEvent("Bang target is empty");
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        #region 桃子
        internal static void GL_Peach(Player source, Player target)
        {
            if (target.GameInfo.CurrentHP >= target.GameInfo.MaxHP)
            {
                RaiseNoticeMessageEvent("Peach Target {0} blood is full", target.PlayerName);
                return;
            }

            RaiseNoticeMessageEvent("{0} 对 {1} 使用桃", source.PlayerName, target.PlayerName);
            Core.GL_Heal(source, target);
        }

        internal static void GL_Peach(Player source, Player target, List<Card> cards)
        {
            if (target.GameInfo.CurrentHP >= target.GameInfo.MaxHP)
            {
                RaiseNoticeMessageEvent("Peach Target {0} blood is full", target.PlayerName);
                return;
            }

            Core.GL_Used(source, cards, true);
            RaiseNoticeMessageEvent("{0} 对 {1} 使用桃", source.PlayerName, target.PlayerName);

            Core.GL_Heal(source, target);
        }
        #endregion

        #region 装备
        internal static void GL_Equip(Player player, CardType cardType, Card card)
        {
            player.GameInfo.TryRemoveCard(card);

            GL_UnEquip(player, true, cardType);

            switch (cardType)
            {
                case CardType.Weapon:
                    player.GameInfo.Weapon = card;
                    break;
                case CardType.Armor:
                    player.GameInfo.Armor = card;
                    break;
                case CardType.AttackHorse:
                    player.GameInfo.AttackHorse = card;
                    break;
                case CardType.DefenseHorse:
                    player.GameInfo.DefenseHorse = card;
                    break;
            }

            RaiseNoticeMessageEvent("{0}装备了{1}", player.PlayerName, card);
        }

        public static List<Card> GL_UnEquip(Player player, bool discard = true, params CardType[] cardTypes)
        {
            List<Card> discards = new List<Card>();

            foreach (var ct in cardTypes)
            {
                Card old = null;// = player.GameInfo.Weapon;
                switch (ct)
                {
                    case CardType.Weapon:
                        old = player.GameInfo.Weapon;
                        break;
                    case CardType.Armor:
                        old = player.GameInfo.Armor;
                        break;
                    case CardType.AttackHorse:
                        old = player.GameInfo.AttackHorse;
                        break;
                    case CardType.DefenseHorse:
                        old = player.GameInfo.DefenseHorse;
                        break;
                }

                if (old != null)
                {
                    GameContext.Current.RaiseNoticeMessageEvent(GameContext.Current, "{0}卸载了装备{1}", player.PlayerName, old.ToString());
                    discards.Add(old);

                    old = null;
                }
                // TODO 触发技能“枭姬”
            }

            // TODO 触发技能“旋风”

            if (discard)
            {
                Core.GL_Discard(player, discards);
                return new List<Card>();
            }
            else
            {
                return discards;
            }
        }

        #endregion
    }
}
